#pragma once

#include <iostream>
#include <unordered_map>
#include <functional>
#include <time.h>
#include <unistd.h>

using work_t=std::function<void()>;


void DownLoad()
{
    std::cout<< "我是一个下载任务" << std::endl;
}
void Sql()
{
    std::cout<< "我是一个数据库同步任务" << std::endl;
}
void Log()
{
    std::cout<< "我是一个日志任务" << std::endl;
}

static int tasknumer = 0;

class task
{
public:
    task()
    {
        srand(time(0));
        InsertTask(DownLoad);
        InsertTask(Sql);
        InsertTask(Log);
    }
    ~task(){}
    void InsertTask(work_t work)
    {
        tasks[tasknumer++] = work;
    }
    int SelectTask()
    {
        return rand() % tasknumer;
    }
    void Execute(int fd)
    {
        if (tasks.find(fd) == tasks.end()) return;
        tasks[fd]();
    }

private:
    std::unordered_map<int,work_t> tasks;
};

task tm;

void Worker()
{
    //
    while (true)
    {
        int cmd = 0;
        ssize_t n = ::read(0, &cmd, sizeof(cmd));
        if (n == sizeof(cmd))
        {
            tm.Execute(cmd);
        }
        else if (n == 0)
        {
            break;
        }
        else
        {
        }
    }
}